package cn.doitedu.rtdw.data_etl.udfs;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.table.functions.ScalarFunction;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.TemporalField;

public class TimeTruncUdf extends ScalarFunction {

    /**
     *
     * @param originTimestamp  : "2023-09-27 10:36:15"
     * @param interval
     * @return
     */
    public String eval(LocalDateTime originTimestamp, Integer interval){
        Instant instant = originTimestamp.toInstant(ZoneOffset.of("+8"));

        long epochSecond = instant.getEpochSecond();

        long tmp = interval*60;
        long trunkedEpochSecond = (epochSecond/tmp)*tmp;

        return DateFormatUtils.format(trunkedEpochSecond*1000,"yyyy-MM-dd HH:mm:ss");
    }


    /*
    public static void main(String[] args) {
        TimeTruncUdf timeTruncUdf = new TimeTruncUdf();
        System.out.println(timeTruncUdf.eval(LocalDateTime.now(), 1));
    }*/


}
